#include "../mqserver/binding.hpp"
#include <gtest/gtest.h>

zhong::BindingManager::ptr binding_manager;

class BindingTest : public testing::Environment
{
public:
    virtual void SetUp() override
    {
        binding_manager = std::make_shared<zhong::BindingManager>("./data/meta.db");
    }
    virtual void TearDown() override
    {
        // binding_manager->clear(); // 清理数据库
    }
};
// 插入测试----已成功
TEST(binding_test, bind_test)
{
    
    binding_manager->bind("exchange1", "queue1", "new.music.#", true);
    binding_manager->bind("exchange1", "queue2", "new.sport.#", true);
    binding_manager->bind("exchange2", "queue1", "new.music.pop", true);
    binding_manager->bind("exchange2", "queue2", "new.sport.football", true);
    binding_manager->bind("exchange2", "queue3", "new.sport.tennis", false);
    ASSERT_EQ(binding_manager->size(), 5);
}
// 查询测试---已成功
TEST(binding_test, select_test)
{
    // 查是否存在
    ASSERT_EQ(binding_manager->exist("exchange1", "queue1"), true);

    // 查绑定
    zhong::Binding::ptr binding = binding_manager->getBinding("exchange1", "queue1");
    ASSERT_EQ(binding->_binding_key, std::string("new.music.#"));
    ASSERT_EQ(binding->_binding_key, std::string("new.music.#"));

    zhong::QueueBindingMap queue_map = binding_manager->getExchangeBindings("exchange1");
    ASSERT_EQ(queue_map.size(), 2);
}
// 解绑定---已成功
TEST(binding_test, unbind_test)
{
    // zhong::QueueBindingMap queue_map = binding_manager->getExchangeBindings("exchange1");
    zhong::Binding::ptr binding = binding_manager->getBinding("exchange1", "queue1");
    binding_manager->unBind("exchange1", "queue1");
    ASSERT_EQ(binding->_binding_key, std::string("new.music.#"));
    // ASSERT_EQ(queue_map.find("queue1"), queue_map.end()); // 如果测试成功，表示不存在queue1
}
// 删除交换机，队列
TEST(binding_test, remove_test)
{
    binding_manager->removeQueueBindings("queue1");
    ASSERT_EQ(binding_manager->exist("exchange1", "queue1"), false);
    ASSERT_EQ(binding_manager->exist("exchange2", "queue1"), false);

    binding_manager->removeExchangeBindings("exchange1");
    ASSERT_EQ(binding_manager->exist("exchange1", "queue1"), false);
    ASSERT_EQ(binding_manager->exist("exchange1", "queue2"), false);

     ASSERT_EQ(binding_manager->size(), 2);
}
int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new BindingTest);
    RUN_ALL_TESTS();
}

